﻿using System.Windows;
using System.Windows.Controls;
using GrainManage.Base;
using GrainManage.Tools;
using GrainManage.Entities.Price;
using GrainManage.Proxy;
using System;

namespace GrainManage.Controls
{
    /// <summary>
    /// Interaction logic for EditPrice.xaml
    /// </summary>
    public partial class EditPrice : UserControl
    {
        private readonly IShowContent UI;

        public EditPrice()
        {
            InitializeComponent();
            UI = new UIHelper(Dispatcher);
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            var inputPrice = GetPriceInfo();
            if (inputPrice != null)
            {
                btnSave.IsEnabled = false;
                if (string.IsNullOrEmpty(txtId.Text))
                {
                    SafeInvoke.TryBeginInvoke(() =>
                    {
                        var input = new InputInsert();
                        input.Price = inputPrice;
                        return Creator.PriceProxy.Insert(input);
                    },
                    (result) =>
                    {
                        btnSave.IsEnabled = true;
                    },
                    (result) =>
                    {
                        if (result.Status.StatusCode != 1)
                        {
                            SafeInvoke.ShowStatus(result.Status.StatusCode, result.Status.Message, Dispatcher);
                        }
                    }, Dispatcher);
                }
                else
                {
                    SafeInvoke.TryBeginInvoke(() =>
                    {
                        var input = new InputUpdate();
                        input.Authentication = App.Authentication;
                        input.Price = inputPrice;
                        return Creator.PriceProxy.Update(input);
                    },
                   (result) =>
                   {
                       btnSave.IsEnabled = true;
                   },
                   (result) =>
                   {
                       if (result.Status.StatusCode != 1)
                       {
                           SafeInvoke.ShowStatus(result.Status.StatusCode, result.Status.Message, Dispatcher);
                       }
                   }, Dispatcher);
                }
            }
        }

        private void BindDropDownEvent()
        {
            var eh = new GrainManage.Tools.EventHelper();
            eh.AddDropDownOpenedHandler(cbPriceType, () =>
            {
                SafeInvoke.TryBeginInvoke(() =>
                {
                    return Creator.PriceProxy.GetPriceType(PageSettings.PriceTypeCount);
                },
                null, (result) =>
                {
                    UI.ShowContent(cbPriceType, result);
                }, Dispatcher);
            });

            eh.AddDropDownOpenedHandler(cbGrainType, () =>
            {
                SafeInvoke.TryBeginInvoke(() =>
                {
                    return Creator.PriceProxy.GetGrainType(PageSettings.GrainTypeCount);
                }, null, (result) =>
                {
                    UI.ShowContent(cbGrainType, result);
                }, Dispatcher);
            });
        }

        private GrainManage.Entities.Table.Price GetPriceInfo()
        {
            GrainManage.Entities.Table.Price inputPrice = null;
            int price = 0;
            if (int.TryParse(tbPrice.Text, out price))
            {
                inputPrice = new GrainManage.Entities.Table.Price();
                if (!string.IsNullOrEmpty(txtId.Text))
                {
                    inputPrice.PriceID = Convert.ToInt32(txtId.Text);
                }
                inputPrice.Grain = cbGrainType.Text;
                inputPrice.PriceType = cbPriceType.Text;
                inputPrice.UnitPrice = price;
                inputPrice.Remarks = tbRemark.Text;
                inputPrice.Creator = App.Authentication.UserName;
            }
            return inputPrice;
        }

        private void SetPriceInfo(GrainManage.Entities.Table.Price price)
        {
            txtId.Text = price.PriceID.ToString();
            tbPrice.Text = price.UnitPrice.ToString();
            tbRemark.Text = price.Remarks;
            cbGrainType.Text = price.Grain;
            cbPriceType.Text = price.PriceType;
        }
    }
}
